import json
import os

from dotenv import load_dotenv

from common.redis import MyRedis

load_dotenv()

REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
REDIS_PORT = os.getenv("REDIS_PORT", 6379)
REDIS_DB = os.getenv("REDIS_DB", 0)
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD", None)
REDIS_PREFIX = os.getenv("REDIS_PREFIX", "fastapi")


def get_vpp_plan_from_redis():
    r = MyRedis(REDIS_HOST, REDIS_PORT, REDIS_DB, REDIS_PASSWORD, REDIS_PREFIX)
    vpp_plan = r.get_custom("vpp_plan")
    return vpp_plan if vpp_plan else {}


def get_vpp_price_from_redis():
    r = MyRedis(REDIS_HOST, REDIS_PORT, REDIS_DB, REDIS_PASSWORD, REDIS_PREFIX)
    vpp_price = r.get_custom("vpp_price")
    return vpp_price if vpp_price else {}


def get_response_plan_from_redis():
    r = MyRedis(REDIS_HOST, REDIS_PORT, REDIS_DB, REDIS_PASSWORD, REDIS_PREFIX)
    response_plan = r.get_custom("response_plan")
    return json.loads(response_plan) if response_plan else []


def set_resource_plan_to_redis(plan):
    r = MyRedis(REDIS_HOST, REDIS_PORT, REDIS_DB, REDIS_PASSWORD, REDIS_PREFIX)
    r.set_custom("response_plan", plan)


def init_invitation():
    r = MyRedis(REDIS_HOST, REDIS_PORT, REDIS_DB, REDIS_PASSWORD, REDIS_PREFIX)
    vpp_plan = r.get_custom("vpp_plan")
    if vpp_plan is None:
        r.set_custom("vpp_plan", "{}")

    vpp_price = r.get_custom("vpp_price")
    if vpp_price is None:
        r.set_custom("vpp_price", "{}")

    response_plan = r.get_custom("response_plan")
    if response_plan is None:
        r.set_custom("response_plan", "[]")
